const { Op } = require("sequelize");
const crypto = require('crypto')
const admin = require('../models/admin')
const role = require('../models/role')
const menu = require('../models/menu')
const { tokenVerification } = require('../middleware/index.js')
const operateObj = require('./utils')

let obj = {
	addData: (body, res) => {
		let md5 = crypto.createHash('md5')
		let pass = md5.update(body.password).digest('hex')
		body.password = pass
		operateObj.add(admin, body, res)
	},
	getPage: (body, res) => {
		operateObj.page(admin, body, res, ['username', 'nickname'])
	},
	getList: (body, res) => {
		operateObj.list(admin, body, res)
	},
	getInfo: (body, res) => {
		operateObj.info(admin, body, res)
	},
	updateData: (body, res) => {
		operateObj.update(admin, body, res)
	},
	deleteData: (body, res) => {
		operateObj.del(admin, body, res)
	},
	getRouter: async (body, res, req) => {
		let user = await tokenVerification(req.headers.token)
		let adminParams = {
			username: user.username
		}
		const adminData = await admin.findOne({ where: adminParams })
		let roleParams = {
			id: adminData.role
		}
		const roleData = await role.findOne({ where: roleParams })
		let menuParams = {
			id: roleData.menu.split(','),
			status: '1',
			[Op.not]: [
				{ type: [3] },
			]
		}
		const menuData = await menu.findAll({ where: menuParams })
		res.json({
			code: 0,
			data: menuData,
			msg: 'success'
		})
	}
}

module.exports = obj
